'use strict';

angular.module('app')
	.controller('qualificationsController', [ '$rootScope', '$scope', '$http', '$state','$timeout',
	                                function($rootScope, $scope, $http, $state,$timeout) {
		$scope.title = '资质审核';
        $scope.param = { };
        $scope.url="https://ifci.lincomb.com";//本地也显示图片
        $scope.loading = false;
		$scope.search = function () {
	        $scope.loading = true;
            var createStartTime = $("#createStartTime").val();
            var createEndTime = $("#createEndTime").val();
            $scope.param.createStartTime = createStartTime;
            $scope.param.createEndTime = createEndTime;
			$.ajax({
				url : $scope.app.project_name + '/qualification/read/list',
				data: $scope.param
			}).then(function(result) {
				console.log(result);
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result.data;
                } else {
					$scope.msg = result.msg;
				}
				$scope.$apply();

            });
		}

		$scope.searchRe = function () {
			$scope.loading = true;
			$scope.param.pageNum = 1;
            var createStartTime = $("#createStartTime").val();
            var createEndTime = $("#createEndTime").val();
            $scope.param.createStartTime = createStartTime;
            $scope.param.createEndTime = createEndTime;
			$.ajax({
				url : $scope.app.project_name + '/qualification/read/list',
				data: $scope.param
			}).then(function(result) {
				$scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result.data;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
		
		$scope.search();
		
		$scope.clearSearch = function() {
            $scope.param= { };
            $("#createStartTime").val("");
            $("#createEndTime").val("");
			// $scope.param.keyword= null;
			$scope.search();
		}
		
		$scope.selected = [];
		var updateSelected = function (action, contact) {
			if (action == 'add' && $scope.selected.indexOf(contact.id+"-"+contact.userIds) == -1){
				$scope.selected.push(contact.id+"-"+contact.userIds);
				contact.checked = true;
			}

			if (action == 'remove' && $scope.selected.indexOf(contact.id+"-"+contact.userIds) != -1){
				$scope.selected.splice($scope.selected.indexOf(contact.id+"-"+contact.userIds), 1);
				contact.checked = false;
			}
		};

		$scope.updateSelection = function ($event, contact) {
			var checkbox = $event.target;
			var action = (checkbox.checked ? 'add' : 'remove');
			if (action == 'add' && $scope.selected.indexOf(contact.id) == -1){
				$scope.selected.push(contact.id+"-"+contact.userIds);
				contact.checked = true;
			}
			if (action == 'remove' && $scope.selected.indexOf(contact.id+"-"+contact.userIds) != -1){
				$scope.selected.splice($scope.selected.indexOf(contact.id+"-"+contact.userIds), 1);
				contact.checked = false;
			}
		};

		// 全选操作
        $scope.selectAll = function ($event) {
        	var checkbox = $event.target;
        	var action = (checkbox.checked ? 'add' : 'remove');
        	for (var i = 0; i < $scope.pageInfo.records.length; i++) {
        		var contact = $scope.pageInfo.records[i];
        		updateSelected(action, contact);
        	}
        };
        
        $scope.isSelected = function (id,userIds) {
        	return $scope.selected.indexOf(id+"-"+userIds) >= 0;
        };
         // 批量通过
		$scope.approve = function(id, state) {

			var ids = [];
            if (id == null || id == '') {
                ids = $scope.selected;
            } else {
                ids.push(id);
            }
            var idStr = ids.join(',');
            if (idStr == null || idStr == '') {
                $scope.alertModal({"content":"请选择需要审核的记录"});
                return;
            }

            //判断是否有待审核的并记录有多少条
            var count = 0;
            var nocount = 0;
            var advertisercount=0
            angular.forEach($scope.pageInfo.records,function (item) {
            	if(item.advertiserStatus != 2 && item.checked){
                    advertisercount++;
				}
                if(item.status == 1 && item.checked){
                    count++;
                }else if(item.checked && item.status == 2 || item.checked && item.status==3) {
                    nocount++;
                }
            })
            if(advertisercount>0){
                $scope.alertModal({"content":"该资质广告主未审核，请先通过广告主审核"});
                return;
            }
            if(nocount>0){
                $scope.alertModal({"content":"待审核"+count+"条,不符合操作条件的"+nocount+"条"});
                return;
            }

			$scope.alertMessage({
				text:"确定做此操作吗？", //内容
				btnCancel:true, //取消按钮
				btnConfirm:true //确认按钮
			}).then(function () {
				var ids = [];

				if (id == null || id == '') {
					ids = $scope.selected;
				} else {
					ids.push(id);
				}

				var idStr = ids.join(',');
				$.ajax({
					url : $scope.app.project_name + '/qualification/batchUpdate',
					data: {'ids': idStr, 'status' : state},
					traditional: true
				}).then(function(result) {
					$scope.loading = false;
					if (result.httpCode == 200) {
                        if(result.data.record!=null){
                            if(result.data.record.resCode==10006){
                                $scope.alertModal({"content":result.data.record.resMsg});
                                $timeout(function () {
                                    $scope.search();
                                }, 1000);
                            }else{
                                $scope.alertModal({"content":result.data.record.resMsg});
                                $timeout(function () {
                                    $scope.search();
                                }, 1000)
                            }
                        }else{
                            $scope.alertModal({"content": "操作成功"});
                            $scope.search();
                        }
						$scope.param.keyword = null;
						$scope.selected = [];
						$scope.search();
					} else {
						$scope.msg = result.msg;
					}
				});
			})
		}
         // 批量上传资质
		$scope.uploadQualify = function(id, state) {
			var ids = [];
            if (id == null || id == '') {
                ids = $scope.selected;
            } else {
                ids.push(id);
            }
            var idStr = ids.join(',');
            if (idStr == null || idStr == '') {
                $scope.alertModal({"content":"请选择需要上传的记录"});
                return;
            }

            //判断是否有待审核的并记录有多少条
            var count = 0;
            var nocount = 0;

            angular.forEach($scope.pageInfo.records,function (item) {
                if(item.status == 2 && item.checked ){
                    count++;
                }else if(item.checked && item.status == 1 || item.checked && item.status==3) {
                    nocount++;
                }
            })
            if(nocount>0){
                $scope.alertModal({"content":"审核成功"+count+"条,不符合操作条件的"+nocount+"条,只有审核成功方可提交第三方"});
                return;
            }
			$scope.alertMessage({
				text:"确定做此操作吗？", //内容
				btnCancel:true, //取消按钮
				btnConfirm:true //确认按钮
			}).then(function () {
				var ids = [];

				if (id == null || id == '') {
					ids = $scope.selected;
				} else {
					ids.push(id);
				}
				var idStr = ids.join(',');
				$.ajax({
					url : $scope.app.project_name + '/qualification/uploadQualify',
					data: {'ids': idStr, 'status' : state},
					traditional: true
				}).then(function(result) {
					console.log(result);
					$scope.loading = false;
					if (result.httpCode == 200) {
						if(result.data.record!=null){
                            if(result.data.record.resCode==10006){
                                $scope.alertModal({"content":result.data.record.resMsg});
                                $timeout(function () {
                                    $scope.search();
                                }, 1000);
                            }else{
                                $scope.search();
                            }
						}else{
                            $scope.alertModal({"content": "操作成功"});
                            $scope.search();
						}
						$scope.param.keyword = null;
						$scope.selected = [];

					} else {
						$scope.msg = result.msg;
					}
				});
			})
		}

		// 逻辑删除
		$scope.delete = function(id, state) {
			var ids = [];
            if (id == null || id == '') {
                ids = $scope.selected;
            } else {
                ids.push(id);
            }
            var idStr = ids.join(',');
            if (idStr == null || idStr == '') {
                $scope.alertModal({"content":"请选择需要审核的记录"});
                return;
            }

            //判断是否有待审核的并记录有多少条
            var count = 0;
            var nocount = 0;
            angular.forEach($scope.pageInfo.records,function (item) {
                if(item.status == 1 && item.checked){
                    count++;
                }else if(item.checked && item.status == 2 || item.checked && item.status==3) {
                    nocount++;
                }
            })
            if(nocount>0){
                $scope.alertModal({"content":"待审核"+count+"条,不符合操作条件的"+nocount+"条"});
                return;
            }

			$scope.alertMessage({
				text:"确定做此操作吗？", //内容
				btnCancel:true, //取消按钮
				btnConfirm:true //确认按钮
			}).then(function () {
				var ids = [];
				if (id == null || id == '') {
					ids = $scope.selected;
				} else {
					ids.push(id);
				}
				$.ajax({
					url : $scope.app.project_name + '/qualification/batchUpdate',
					data: {'ids': idStr, 'status' : state},
					traditional: true
				}).then(function(result) {

					$scope.loading = false;
					if (result.httpCode == 200) {
                        $scope.alertModal({"content": "操作成功"});
						$scope.param.keyword = null;
						$scope.selected = [];
						$scope.search();
					} else {
						$scope.msg = result.msg;
					}
				});
			})
		}
       // 批量失败
		$scope.refuseAdverts = function (id, state) {
			$scope.userAdverts = {};
			var ids = [];
			if (id == null || id == '') {
				ids = $scope.selected;
			} else {
				ids.push(id);
			}
			var idStr = ids.join(',');
			if (idStr == null || idStr == '') {
				$scope.alertModal({"content":"请选择需要审核的记录"});
				return;
			} else {

                //判断是否有待审核的并记录有多少条
                var count = 0;
                var nocount = 0;
                var advertisercount=0
                angular.forEach($scope.pageInfo.records,function (item) {
                    if(item.advertiserStatus != 2 && item.checked){
                        advertisercount++;
                    }
                    if(item.status == 1 && item.checked){
                        count++;
                    }else if(item.checked && item.status != 1) {
                        nocount++;
                    }
                })
                if(advertisercount>0){
                    $scope.alertModal({"content":"该资质广告主未审核，请先通过广告主审核"});
                    return;
                }
                if(nocount>0){
                    $scope.alertModal({"content":"待审核"+count+"条,不符合操作条件的"+nocount+"条"});
                    return;
                }

				$scope.userAdverts.ids = idStr;
				$scope.userAdverts.status = state;

				$('#refuseUserAdverts').modal('show', true);
			}
		}

		$scope.saveUserAdverts = function () {
			var approveReason = $("#approveReason").val();
			if (approveReason == "" || approveReason == null) {
                $scope.alertModal({"content":"审核失败原因不能为空"});
                $('#refuseUserAdverts').modal('hide');
				return;
			}
            $scope.userAdverts.approveReason=approveReason;
			$.ajax({
				url : $scope.app.project_name + '/qualification/batchUpdate',
				data: $scope.userAdverts
			}).then(function(result) {
				$scope.loading = false;
				if (result.httpCode == 200) {
					$('#refuseUserAdverts').modal('hide');
					$scope.alertModal({"content":"操作成功"});

					$scope.param.keyword = null;
					$scope.selected = [];
					$scope.search();
				} else {
					$('#refuseUserAdverts').modal('hide');
					$scope.alertModal({"content":"操作失败"});
                    $timeout(function () {
                        $scope.search();
                    }, 1000);
				}
			});
		}


		
		// 翻页
        $scope.pagination = function (page) {
            $scope.param.pageNum=page;
            $scope.search();
        };

		$scope.qualifyLogo = [];
		$scope.preview = function(qualifyLogo){
			if (qualifyLogo != null && qualifyLogo != '') {
				$scope.qualifyLogo = qualifyLogo.split('|');
			}
			$scope.firstApply = true;
		}

		// $scope.preview1 = function(id){
		// 	$scope.firstApply1 = true;
         //    $scope.record = {'adId' :id};
		// }

} ]);